package com.ganmiao.server.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ganmiao.resp.PageResult;
import com.ganmiao.server.user.entity.Customer;
import com.ganmiao.server.user.query.CustomerQuery;
import com.ganmiao.server.user.repository.CustomerRepository;
import com.ganmiao.utils.StatusUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author calms
 * @Date 2024/4/1
 * @Desc
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    /**
     * 列表
     */
    public PageResult<Customer> customerPageResult(CustomerQuery query){
        LambdaQueryWrapper<Customer> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Customer::getStatus, StatusUtils.PASS);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(query.getName()),Customer::getName,query.getName());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(query.getPhone()),Customer::getPhone,query.getPhone());
        lambdaQueryWrapper.orderByDesc(Customer::getId);
        return customerRepository.queryPage(query.getPage(),query.getPageSize(),lambdaQueryWrapper);
    }

    /**
     * 详情
     */
    public Customer getId(Integer id){
        return customerRepository.getById(id);
    }

    /**
     * 增加
     */
    public boolean add(Customer customer){
        return customerRepository.save(customer);
    }

    /**
     * 修改
     */
    public boolean put(Customer customer){
        return customerRepository.updateById(customer);
    }
}
